require 'config/boot'
require 'config/environment'
require 'lib/simple-json'

task :tag_list do
  @pos = Segment.find(:all,:select => :part_of_speech,:group => :part_of_speech,:order => :part_of_speech)
  @cform = Segment.find(:all,:select => :cform,:group => :cform,:order => :cform)
  @ctype = Segment.find(:all,:select => :ctype,:group => :ctype,:order => :ctype)
  @pos.map!{|item| item.part_of_speech}.compact!
  @ctype.map!{|item| item.ctype}.compact!
  @cform.map!{|item| item.cform}.compact!
  
  @tags = [@pos,@ctype,@cform]
  @tags.map! do |tag| 
    tag.map! do |item|
      parts = item.split('-')
      m = []
      parts.each_with_index do |part,index|
        m << parts[0..index].join('-')
      end
      m
    end
    tag.flatten.uniq.sort.map{|item| {"item" => item}}
  end
  @tags.map!{|tag| {"list" => tag}}
  
  @dir = "public/config/"
  @builder = JsonBuilder.new
  ["pos.json","ctype.json","cform.json"].each_with_index do |name,index|
    f = File.open(@dir + name,'w')
    f.puts @builder.build(@tags[index])
    f.close
  end
end